package com.hurt.competition.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hurt.competition.admin.dto.NoticeReq;
import com.hurt.competition.admin.mapper.NoticeMapper;
import com.hurt.competition.admin.model.Notice;
import com.hurt.competition.admin.service.NoticeService;
import com.hurt.competition.common.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Override
    public PageResult<Notice> queryList(NoticeReq req) {
        // 开启分页，只针对遇到的第一个对数据库的查询分页
        Page<Notice> page = PageHelper.startPage(req.getPageNumber(), req.getPageSize());

        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊查询，like方法的第一个参数为条件，如果为true，则进行后续的操作
        queryWrapper.like(StringUtils.isNotBlank(req.getTitle()), Notice::getTitle, req.getTitle());

        list(queryWrapper);

        // 直接返回page，因为数据在page里封装好了
        PageResult pageResult = new PageResult();
        pageResult.setList(page.getResult());
        pageResult.setTotal(page.getTotal());
        return pageResult;

    }

    @Override
    public Notice queryInfo(NoticeReq req) {
        return this.getById(req.getId());
    }
}
